#include "postcardwidget.h"
#include <QMouseEvent>
#include <QStyle>
#include <QGraphicsDropShadowEffect>
#include <QDateTime> // For date formatting

namespace WordMemory {

PostCardWidget::PostCardWidget(const CommunityPost &post, QWidget *parent)
    : QWidget(parent), m_post(post)
{
    setupUi();

    titleLabel->setText(m_post.title);
    authorLabel->setText(QString("作者: %1").arg(m_post.authorUsername));

    QString contentPreview = m_post.content;
    if (contentPreview.length() > 100) {
        contentPreview = contentPreview.left(100) + "...";
    }
    previewLabel->setText(contentPreview);

    // Format date/time
    QDateTime createdAt = QDateTime::fromString(m_post.createdAt, Qt::ISODate);
    if (!createdAt.isValid()) {
        createdAt = QDateTime::fromString(m_post.createdAt, "yyyy-MM-dd HH:mm:ss");
    }
    timeLabel->setText(createdAt.isValid() ? createdAt.toString("yyyy-MM-dd hh:mm") : m_post.createdAt);


    likesCountLabel->setText(QString::number(m_post.likesCount));
    sharesCountLabel->setText(QString::number(m_post.sharesCount));

    likeButton->setIcon(QIcon(m_post.isLikedByCurrentUser ? ":/icons/like_filled_icon.png" : ":/icons/like_outline_icon.png")); // Add these icons to resource.qrc
    likeButton->setText(m_post.isLikedByCurrentUser ? "已赞" : "点赞");


    connect(likeButton, &QPushButton::clicked, this, [this](){ emit likeClicked(m_post.id); });
    connect(shareButton, &QPushButton::clicked, this, [this](){ emit shareClicked(m_post.id); });

    // Styling
    this->setStyleSheet(R"(
        PostCardWidget {
            background-color: #ffffff;
            border: 1px solid #e8e8e8;
            border-radius: 8px;
            padding: 15px;
            margin-bottom: 10px; /* Spacing between cards */
        }
        QLabel#titleLabel {
            font-size: 16px;
            font-weight: bold;
            color: #333333;
        }
        QLabel#authorLabel, QLabel#timeLabel {
            font-size: 12px;
            color: #888888;
        }
        QLabel#previewLabel {
            font-size: 14px;
            color: #555555;
            margin-top: 5px;
            margin-bottom:10px;
        }
        QPushButton {
            background-color: transparent;
            border: none;
            font-size: 13px;
            color: #555555;
            padding: 5px;
        }
        QPushButton:hover {
            color: #1890ff;
        }
        QLabel#likesCountLabel, QLabel#sharesCountLabel {
            font-size: 13px;
            color: #555555;
            margin-left: -2px; /* Adjust spacing with icon */
            margin-right: 10px;
        }
    )");

    this->setMinimumHeight(120);
}

PostCardWidget::~PostCardWidget()
{

}

void PostCardWidget::setupUi() {

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(8);


    titleLabel = new QLabel();
    titleLabel->setObjectName("titleLabel");
    titleLabel->setWordWrap(true);
    mainLayout->addWidget(titleLabel);

    QHBoxLayout *subHeaderLayout = new QHBoxLayout();
    authorLabel = new QLabel();
    authorLabel->setObjectName("authorLabel");
    timeLabel = new QLabel();
    timeLabel->setObjectName("timeLabel");
    timeLabel->setAlignment(Qt::AlignRight);
    subHeaderLayout->addWidget(authorLabel);
    subHeaderLayout->addStretch();
    subHeaderLayout->addWidget(timeLabel);
    mainLayout->addLayout(subHeaderLayout);

    previewLabel = new QLabel();
    previewLabel->setObjectName("previewLabel");
    previewLabel->setWordWrap(true);
    previewLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
    mainLayout->addWidget(previewLabel);

    mainLayout->addStretch(1);

    QHBoxLayout *actionsLayout = new QHBoxLayout();
    actionsLayout->setSpacing(0);

    likeButton = new QPushButton();
    // likeButton->setIcon(QIcon(":/icons/like_outline_icon.png"));
    likeButton->setIconSize(QSize(16,16));
    likesCountLabel = new QLabel();
    likesCountLabel->setObjectName("likesCountLabel");

    shareButton = new QPushButton("分享");
    shareButton->setIcon(QIcon(":/icons/share_icon.png"));
    shareButton->setIconSize(QSize(16,16));
    sharesCountLabel = new QLabel();
    sharesCountLabel->setObjectName("sharesCountLabel");

    actionsLayout->addWidget(likeButton);
    actionsLayout->addWidget(likesCountLabel);
    actionsLayout->addStretch(1);
    actionsLayout->addWidget(shareButton);
    actionsLayout->addWidget(sharesCountLabel);

    mainLayout->addLayout(actionsLayout);
    setLayout(mainLayout);
}


void PostCardWidget::updateLikeState(int newLikesCount, bool isLikedByMe) {
    m_post.likesCount = newLikesCount;
    m_post.isLikedByCurrentUser = isLikedByMe;
    likesCountLabel->setText(QString::number(m_post.likesCount));
    likeButton->setIcon(QIcon(m_post.isLikedByCurrentUser ? ":/icons/like_filled_icon.png" : ":/icons/like_outline_icon.png"));
    likeButton->setText(m_post.isLikedByCurrentUser ? "已赞" : "点赞");
}

void PostCardWidget::updateShareCount(int newSharesCount) {
    m_post.sharesCount = newSharesCount;
    sharesCountLabel->setText(QString::number(m_post.sharesCount));
}


void PostCardWidget::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {

        if (!likeButton->geometry().contains(event->pos()) &&
            !shareButton->geometry().contains(event->pos())) {
            emit cardClicked(m_post.id);
        }
    }
    QWidget::mousePressEvent(event);
}

} // namespace WordMemory
